﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SD.LLBLGen.Pro.ApplicationCore;
using SD.LLBLGen.Pro.ApplicationCore.EntityModel;
using SD.LLBLGen.Pro.ApplicationCore.Extensibility;
using SD.LLBLGen.Pro.ApplicationCore.Interfaces;
using SD.LLBLGen.Pro.ApplicationCore.Mapping;
using SD.LLBLGen.Pro.ApplicationCore.ProjectClasses;
using SD.LLBLGen.Pro.DBDriverCore;

namespace SD.LLBLGen.Pro.Plugins
{
    public partial class EntityVisualizerControl : Form, IPluginWindow
    {
        private const string driverId = "2D18D138-1DD2-467E-86CC-4838250611AE";

        private string _connectionString;
        private PluginBase _plugin;
        private string _entityName;

        public EntityVisualizerControl()
        {
            InitializeComponent();
        }

        public EntityVisualizerControl(ClassGeneratorPlugIn plugin)
        {
            InitializeComponent();
            _plugin = plugin;
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            EntityDefinition entity = _plugin.Entities[0];
            List<IFieldElementCore> llist = entity.GetAllFieldsForVisualization();
            StringBuilder builder=new StringBuilder();
            builder.AppendLine(string.Format("public class {0}", entity.FullName.Replace(".","_")));
            builder.AppendLine("{");
            foreach (IFieldElementCore element in llist)
            {
                builder.AppendLine(string.Format("  public {0} {1};", ProcessTypeName(element.FieldType), element.FullName));
            }
            builder.AppendLine("}");
            txtOutput.Text = builder.ToString();
        }

        private string ProcessTypeName(IFieldType type)
        {
            //string (System.String) 
            string result = string.Empty;
            int index = type.TypeName.IndexOf('(');
            if (index > 0)
            {
                result = type.TypeName.Substring(index + 1);
                result = result.Substring(0, result.Length - 1);
            }
            else
                result= type.TypeName;
            return result;
        }

        public Guid FormID
        {
            get
            {
                return new Guid("{08F8C6A0-30B8-479f-BFB0-95139E6C7B3A}");
            }
            set
            {
                throw new Exception("The method or operation is not implemented.");
            }
        }

        /// <summary>
        /// The ID of the plugin which opened the IPluginWindow instance.
        /// </summary>
        /// <value></value>
        public Guid PluginID
        {
            get
            {
                return _plugin.Describe().Id;
            }
            set
            {
                throw new Exception("The method or operation is not implemented.");
            }
        }

        private void btnCopy_Click(object sender, EventArgs e)
        {
            Clipboard.SetText(txtOutput.Text);
        }
    }
}



